/*
 * Copyright 2011 Noctarius (Christoph Engelbert)
 * Part of www.sourceprojects.org 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may 
 * not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 *    http://www.apache.org/licenses/LICENSE-2.0 
 *
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 */
package org.sourceprojects.cappadocia.builder;

import org.sourceprojects.cappadocia.AnnotationInfo;
import org.sourceprojects.cappadocia.ClassInfo;
import org.sourceprojects.cappadocia.org.josql.functions.AbstractFunctionHandler;
import org.sourceprojects.cappadocia.org.josql.functions.NotFixedResults;

public class CustomJoSQLFunctions extends AbstractFunctionHandler implements
		NotFixedResults {

	public ClassInfo annotatedWith(final String annotation) {
		final Object obj = q.getCurrentObject();
		if (obj instanceof ClassInfo && annotation != null) {
			final ClassInfo classInfo = (ClassInfo) obj;

			for (final AnnotationInfo a : classInfo.getAnnotations()) {
				if (annotation.equals(a.getAnnotationType())) {
					return (ClassInfo) obj;
				}
			}
		}

		return null;
	}

	public String classname() {
		final Object obj = q.getCurrentObject();
		if (obj instanceof ClassInfo) {
			return ((ClassInfo) obj).getCanonicalName();
		}

		return null;
	}

	public ClassInfo implementationOf(final String classname) {
		final Object obj = q.getCurrentObject();
		if (obj instanceof ClassInfo && classname != null) {
			final ClassInfo classInfo = (ClassInfo) obj;

			return implementationOf(classInfo, classInfo, classname);
		}

		return null;
	}

	private ClassInfo implementationOf(final ClassInfo value,
			final ClassInfo testType, final String classname) {

		for (final ClassInfo interfaze : testType.getInterfaces()) {
			if (classname.equals(interfaze.getCanonicalName())) {
				return value;
			}
		}

		if (testType.getSuperclass() != null) {
			return implementationOf(value, testType.getSuperclass(), classname);
		}

		return null;
	}

	public ClassInfo subclassOf(final String classname) {
		final Object obj = q.getCurrentObject();
		
		if (obj instanceof ClassInfo && classname != null) {
			final ClassInfo classInfo = (ClassInfo) obj;
			
			return subclassOf(classInfo, classInfo, classname);
		}

		return null;
	}

	private ClassInfo subclassOf(final ClassInfo value,
			final ClassInfo testType, final String classname) {

		if (testType.getSuperclass() == null) {
			return null;
		}

		if (testType.getCanonicalName().equals(testType.getSuperclass().getCanonicalName())) {
			return null;
		}
		
		if (value.getCanonicalName().equals(classname)) {
			return null;
		}

		if (classname.equals(testType.getSuperclass().getCanonicalName())) {
			return value;
		}

		return subclassOf(value, testType.getSuperclass(), classname);
	}

}
